package Student;

import javax.swing.*;
import java.sql.*;
import java.util.Vector;

public class S_selsc extends JFrame {
    Vector rowData, columnNames;
    Statement stmt=null;
    String sql=null;
    JTable jt = null;
    JScrollPane jsp = null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    public S_selsc(String sql1){
        columnNames = new Vector();
// 设置列名
        columnNames.add("课程号");
        columnNames.add("课程名");
        columnNames.add("学  号");
        columnNames.add("姓  名");
        columnNames.add("成  绩");
        rowData=new Vector();
        sql=sql1;
        try{
            Connection dbConn1=CONN();
            stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs=stmt.executeQuery(sql);
            while(rs.next()){
                Vector hang = new Vector();
                hang.add(rs.getString("kecheng_id"));System.out.print(rs.getString("kecheng_id"));
                hang.add(rs.getString("mingcheng"));System.out.print(rs.getString("Course_name"));
                hang.add(rs.getString("xuesheng_id"));System.out.print(rs.getString("Student_id"));
                hang.add(rs.getString("xingming"));System.out.print(rs.getString("xingming"));
                hang.add(rs.getString("null"));System.out.println(rs.getString("Score"));
                rowData.add(hang);}
            jt=new JTable(rowData,columnNames);
            jsp=new JScrollPane(jt);
            this.add(jsp);
            this.setSize(400,300);
            this.setVisible(true);
        }catch(SQLException e1){
            System.out.print("SQL Exception occur.Message is:"+e1.getMessage());
        }
    }
    // 连接数据库方法
    public static Connection CONN(){
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加 载JDBC 驱动
        String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=master"; //连接服务器和数据库 test
        String userName = "sa"; //默认用户名
        String userPwd = "123456"; //密码
        Connection dbConn=null;
        try {
            Class.forName(driverName);
            dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("Connection Successful!"); //如果连接成功 控制台 输出Connection Successful!
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dbConn;
    }
}
